<template>
  <div id="home">
    <nav-bar class="homenav">
      <div slot="center">购物街</div>
    </nav-bar>
    
   <tab-control :titles= "['流行','新款','精选']" class="tabcontrol" @tabClick="tabClick" ref="tabControl1"
         v-show="isfiexd"/>

    <better-scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll" 
    :pull-up-load="true" @pullingup="loadMore" >
          <!-- 轮播图 -->
        <home-swiper :banner="banner" @swiperImgLoad="swiperImgLoad"></home-swiper>

        <!-- 推荐 -->
        <home-recommend-view :recommends="recommends"></home-recommend-view>

        <!-- 本周流行 -->
        <home-feature/>

        <tab-control :titles= "['流行','新款','精选']" class="tabcontrol" @tabClick="tabClick" ref="tabControl2"
        />

        <goods-list :goods="showGoods"/>
    </better-scroll>

    <!-- 回到顶部 -->
    <back-top @click.native="backClick" v-show="currentShow"/>
  </div>
</template>

<script>

import {getHomeMultidata, getHomeData} from 'network/home'

import NavBar from 'components/common/navbar/NavBar'
import betterScroll from 'components/common/scroll/betterScroll'

import TabControl from 'components/contents/tabControl/TabControl'
import GoodsList from 'components/contents/goods/GoodsList'
import backTop from 'components/contents/backtop/backTop'

import HomeSwiper from './childComps/HomeSwiper'
import HomeRecommendView from './childComps/HomeRecommendView'
import HomeFeature from './childComps/HomeFeature'

import {debounce} from 'common/utils'

export default {
  name: 'home', 
  components:{
    NavBar,
    TabControl,
    betterScroll,
    HomeSwiper,
    HomeRecommendView,
    HomeFeature,
    GoodsList,
    backTop
  },
  data(){
    return{
      banner: [],
      recommends: [],
      keywords: [],
      goods:{
        'pop':{page:0, list:[]},
        'new':{page:0, list:[]},
        'sell':{page:0, list:[]}
      },
      currentType: 'pop',
      currentShow: false,
      offsetTop: 0,
      isfiexd:false,
      saveY: 0
    }
  },
  computed: {
    showGoods(){
      return this.goods[this.currentType].list
    }
  },
  created(){
    //1.请求多个数据
    this.getHomeMultidata()
    //2.请求商品数据
    this.getHomeData('pop')
    this.getHomeData('new')
    this.getHomeData('sell')
  },
  destroyed(){
    console.log('home destroyed')
  },
  activated(){
    this.$refs.scroll.scroll.scrollTo(0, this.saveY, 0)
    this.$refs.scroll.scroll.refresh()
  },
  deactivated(){
    this.saveY = this.$refs.scroll.getScrollY()
    console.log(this.saveY)
  },
  mounted() {  //事件总线bus
    //1.图片加载完成的时间监听
    const refresh = debounce(this.$refs.scroll.refresh,200)
    this.$bus.$on('itemImageLoad', () => {
      refresh()
    })

    
  },
  methods:{
    /**
     * 事件监听相关的方法
     */
    
    tabClick(index){
      //console.log(index)
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1: 
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    backClick(){
      this.$refs.scroll.scroll.scrollTo(0, 0, 500)
    },
    contentScroll(position){
      //判断backtop是否显示
      if(position.y < -200){
        this.currentShow = true
      }else{
        this.currentShow = false
      }
      //决定tabControl是否吸顶（position：fixed）
      this.isfiexd = -(position.y) > this.offsetTop;
    },
    loadMore(){
      //console.log('下拉加载更多');
      this.getHomeData(this.currentType);
      //重新计算content的高度
      this.$refs.scroll.scroll.refresh() 
    },
    swiperImgLoad(){
      //

    //获取tabControl的offsetTop
    //所有的组件都有一个属性$el：用于获取组件的元素
    this.offsetTop = this.$refs.tabControl2.$el.offsetTop;
    },
    /**
     * 网络请求相关的方法
     */
    getHomeMultidata(){
      getHomeMultidata().then(res => {
      //console.log(res);
      this.banner = res.data.banner.list;
      this.recommends = res.data.recommend.list;
      this.keywords = res.data.keywords.list;
    })
    },
    getHomeData(type){
      const page = this.goods[type].page + 1
      getHomeData(type,page).then(res =>{
        //console.log(res.data.list)
      this.goods[type].list.push(...res.data.list)
      this.goods[type].page += 1
      this.$refs.scroll.scroll.finishPullUp()
    })
    }
     
  }
}
</script>

<style scoped>
  #home{
    height: 100vh;
  }
  .homenav{
    background: yellow;
    font-size: 25px;
    color: 	MediumBlue;
    font-weight: bold;
    /* 导航栏定位 */
    /* position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 9 */
  }
  .tabcontrol{
    position: relative;
    z-index: 9;
  }
  .content{
    /* margin-top: 49px; */
    height: calc(100% - 98px);
    overflow: hidden;
  }
  /* .fixed{
    position: fixed;
    left: 0;
    right: 0;
    top: 49px;
  } */
</style>